<template>
  <div>
  <!-- 按钮和搜索栏容器 -->
  <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
    <div style="display: flex; gap: 5px;">
      
      <el-button v-show="returnButton" @click="returnLists()"><el-icon><Back /></el-icon></el-button>
    </div>
    <!-- 搜索栏 -->
    <div>
      <el-form :inline="true">
       <el-form-item label="开始时间">
         <el-date-picker
             v-model="startTime"
             type="datetime"
             placeholder="请选择开始时间"
             value-format="YYYY-MM-DD hh:mm:ss"
         />
        </el-form-item>
        <el-form-item label="结束时间">
           <el-date-picker
           v-model="endTime"
           type="datetime"
           placeholder="请选择开始时间"
           value-format="YYYY-MM-DD HH:mm:ss"
           @change="search()"
           />
         </el-form-item>
         <el-form-item>
           <el-input
             v-model="searchname"
             placeholder="请输入会议室名称/地点"
             style="width: 250px;"
             @keyup.enter="search()"
             >
             <template #append>
               <el-button @click="search()"><el-icon><Search /></el-icon></el-button>
             </template>
           </el-input>
          </el-form-item>
       </el-form>
       
      
    </div>
  </div>

  <el-table id="mytable" :data="filteredData" stripe border style="width: 100%">
    <el-table-column fixed prop="id" label="会议室id" width="80" align="center"/>
    <el-table-column label="会议室名称" width="150" align="center">
      <template #default="scope">
          <img style="width: 100px;height: 100px;" :src="scope.row.firstImageUrl" alt="会议室图片">
       </template>
    </el-table-column>
    <el-table-column prop="name" label="会议室名称" width="150" align="center"/>
    <el-table-column prop="type" label="会议室类型" align="center">
      <template #default="scope">
           {{ scope.row.type==='classroom' ? '教室型' : '圆桌型' }}
      </template>
    </el-table-column>
    <el-table-column prop="capacity" label="容量" align="center"/>
    <el-table-column prop="pricePerHour" label="每小时租赁价格" align="center"/>
    <el-table-column prop="area" label="面积" align="center"/>
    <el-table-column prop="location" label="位置" align="center"/>
    <el-table-column label="操作" width="180" align="center">
      <template v-slot="scope">
        <el-button link @click="openStatusView(scope.row)" type="primary" size="big">查看状态</el-button>
        </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <!-- 分页组件 -->
  <el-pagination
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-size="pageSize"
    :total="totalCount"
  >
  </el-pagination>
   <!-- 状态查看对话框 -->
    <el-dialog
      v-model="dialogVisible"
      title="会议室状态时间"
      width="50%"
      center
    >
      <div class="horizontal-timeline-container">
        <el-timeline>
          <el-timeline-item
            v-for="(item, index) in statusList"
            :key="index"
            :timestamp="`${item.startTime} - ${item.endTime}`"
            :color="getStatusColor(item.status)"
          >
            <div class="status-content">
              <div class="status-title">状态: {{ formatStatus(item.status) }}</div>
              <div class="status-time">时间: {{ item.startTime }} - {{ item.endTime }}</div>
            </div>
          </el-timeline-item>
        </el-timeline>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import { Search } from '@element-plus/icons-vue';
import { Back } from "@element-plus/icons-vue";
export default {
  name: "LostAndFoundList",
  data() {
    return {
      listData: [],
      filteredData: [],
      editIndex: -1,
      searchname: '',
      returnButton:false,
      currentPage:1,
      pageSize:10,
      totalCount:0,
      currentImages: [],          
      currentImageIndex: 0,   
      startTime:'',
      endTime:'',
      dialogVisible:false,
      statusList:[],
      statusColorMap: {
        'idle': 'green',
        'locked': 'gray',
        'in_use': 'red',
        'reserved': 'blue',
        'maintenance': 'yellow'
      }
    }
  },
  components :{
     Search,
     Back,
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    handleCurrentChange(page){
        this.currentPage=page;
        if(!this.searchname){
        this.fetchData();
        }else{
          this.search();
        }
    },
    fetchData(){
        axios.get("/staff/room-status",{
          params:{
            pageNum:this.currentPage,
            pageSize:this.pageSize,
          }
        })
        .then(res=>{
          this.listData=res.data.data.records;
          this.totalCount=res.data.data.total;
          if (Array.isArray(this.listData)) {
            this.filteredData = this.listData;
          } else {
              console.error('加载错误');
          }
        }).catch(err=>{
          this.$message.error(err.data.code||"获取列表失败");
        })
    },
    search() {
      this.returnButton=true;
      axios.get('/staff/room-status',{
          params:{
            keyword:this.searchname,
            pageNum:this.currentPage,
            pageSize:this.pageSize,
            startTime:this.startTime,
            endTime:this.endTime,
          }
        })
      .then(res =>{
        if(res.data.code===200){
          this.filteredData=res.data.data.records;
          this.totalCount=res.data.data.total;
          this.$message.success("查询成功");
        }else {
          this.$message.error(res.data.msg||"查询失败");
        }
      })
    },
    returnLists(){
      this.returnButton=false;
      this.searchname=null;
      this.startTime=null;
      this.endTimenull=null;
      this.currentPage=1;
      this.fetchData();
    },
    goBackToList() {
      this.fetchData();
    },
    openStatusView(row){
      this.statusList=row.status;
      this.dialogVisible=true;       
    },
    formatStatus(status){
      if(status==='idle'){
        return '空闲';
      }else if(status==='locked'){
        return '锁定';
      }else if(status==='in_use'){
        return '使用中';
      }else if(status==='reserved'){
        return '预订';
      }else if(status==='maintenance'){
        return '维护';
      }
    },
    getStatusColor(status){
      return this.statusColorMap[status] || 'info';
    },
    formatCreateTime(time){
      return time.replace('T',' ');
    },
  },
}

</script>

<style scoped>
.clicked-s {
  background-color: green;
  color: white;
}
.image-viewer {
  text-align: center;
}
.viewer-image {
  max-width: 100%;
  max-height: 400px;
  cursor: zoom-in;
}
.image-thumbs {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 10px;
  overflow-x: auto;
  padding: 5px 0;
}
.thumb-item {
  width: 80px;
  height: 80px;
  cursor: pointer;
  border: 2px solid transparent;
  transition: all 0.3s;
}
.thumb-item:hover {
  border-color: #409EFF;
}
.thumb-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.active {
  border-color: #409EFF !important;
}

</style>